//
//  QLNiceLaunchViewController.m
//  Nice
//
//  Created by huangyueqi on 2017/8/1.
//  Copyright © 2017年 sjyt. All rights reserved.
//

#import "QLNiceLaunchViewController.h"
#import "QLNiceNetworkTools.h"
#import "QLNiceHomeViewController.h"

@interface QLNiceLaunchViewController ()

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation QLNiceLaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    //关闭定时器
    [self.timer setFireDate:[NSDate distantFuture]];
}

#pragma mark - SetupUI
- (void)setupUI {
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(launchAction) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
    
    UIView *orangeView = [self createCycleViewForRect:CGRectMake(60, SCREEN_HEIGHT - 100, 20, 20) viewColor:RGB(255, 165, 0) radius:10];
    [self.view addSubview:orangeView];
    UIView *greenView = [self createCycleViewForRect:CGRectMake(SCREEN_WIDTH - 120, SCREEN_HEIGHT - 100, 16, 16) viewColor:RGB(20, 206, 188) radius:8];
    [self.view addSubview:greenView];
    UIView *magentaView = [self createCycleViewForRect:CGRectMake(SCREEN_WIDTH - 50, SCREEN_HEIGHT - 130, 24, 24) viewColor:RGB(189, 16, 224) radius:12];
    [self.view addSubview:magentaView];

    CAShapeLayer *orangeArcLayer = [CAShapeLayer layer];
    orangeArcLayer.frame     = CGRectMake(0, SCREEN_HEIGHT - 60, SCREEN_WIDTH * 0.5, 60);
    orangeArcLayer.fillRule  = kCAFillRuleEvenOdd;
    orangeArcLayer.fillColor = [UIColor colorWithRed:1 green: 0.647 blue:0 alpha:1].CGColor;
    orangeArcLayer.lineWidth = 0;
    orangeArcLayer.path      = [self orangeArcPath].CGPath;
    [self.view.layer addSublayer:orangeArcLayer];
    
    CAShapeLayer *curveLayer = [CAShapeLayer layer];
    curveLayer.frame     = CGRectMake(0, SCREEN_HEIGHT - 90, SCREEN_WIDTH, 90);
    curveLayer.fillColor = [UIColor colorWithRed:0 green: 0.786 blue:0.492 alpha:0.5].CGColor;
    curveLayer.lineWidth = 0;
    curveLayer.path      = [self curvePath].CGPath;
    [self.view.layer addSublayer:curveLayer];
    
    CAShapeLayer *blueArcLayer = [CAShapeLayer layer];
    blueArcLayer.frame     = CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40);
    blueArcLayer.fillRule  = kCAFillRuleEvenOdd;
    blueArcLayer.fillColor = [UIColor colorWithRed:0 green: 0.784 blue:1 alpha:0.5].CGColor;
    blueArcLayer.lineWidth = 0;
    blueArcLayer.path      = [self blueArcPath].CGPath;
    [self.view.layer addSublayer:blueArcLayer];
}

- (void)launchAction {
    
    QLNiceHomeViewController *vc = [[UIStoryboard storyboardWithName:@"QLNice" bundle:nil] instantiateViewControllerWithIdentifier:@"QLNiceHomeViewController"];
    [self presentViewController:vc animated:YES completion:nil];
    
    [self.timer invalidate];
    self.timer = nil;
}

- (UIView *)createCycleViewForRect:(CGRect)rect viewColor:(UIColor *)color radius:(CGFloat)radius {
    
    UIView *cycleView = [[UIView alloc] initWithFrame:rect];
    cycleView.backgroundColor = color;
    cycleView.layer.cornerRadius = radius;
    cycleView.layer.masksToBounds = YES;
    return cycleView;
}

- (UIBezierPath *)orangeArcPath{
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 40)];
    [path addCurveToPoint:CGPointMake(SCREEN_WIDTH * 0.22, 0) controlPoint1:CGPointMake(18, 16) controlPoint2:CGPointMake(SCREEN_WIDTH * 0.13, 0)];
    [path addCurveToPoint:CGPointMake(SCREEN_WIDTH * 0.5, 60) controlPoint1:CGPointMake(SCREEN_WIDTH * 0.33, 0) controlPoint2:CGPointMake(SCREEN_WIDTH * 0.42, 25)];
    [path addLineToPoint:CGPointMake(0, 60)];
    [path addLineToPoint:CGPointMake(0, 40)];
    [path closePath];
    [path moveToPoint:CGPointMake(0, 40)];
    return path;
}

- (UIBezierPath *)curvePath{
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 90)];
    [path addLineToPoint:CGPointMake(SCREEN_WIDTH, 90)];
    [path addLineToPoint:CGPointMake(SCREEN_WIDTH, 0)];
    [path addCurveToPoint:CGPointMake(117, 70) controlPoint1:CGPointMake(307, 0) controlPoint2:CGPointMake(190, 75)];
    [path addCurveToPoint:CGPointMake(0, 0) controlPoint1:CGPointMake(45, 63) controlPoint2:CGPointMake(26, 0)];
    [path closePath];
    [path moveToPoint:CGPointMake(0, 90)];
    return path;
}

- (UIBezierPath *)blueArcPath{
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 37)];
    [path addCurveToPoint:CGPointMake(187, 0) controlPoint1:CGPointMake(SCREEN_WIDTH * 0.15, 13) controlPoint2:CGPointMake(SCREEN_WIDTH * 0.3, 0)];
    [path addCurveToPoint:CGPointMake(SCREEN_WIDTH, 37) controlPoint1:CGPointMake(SCREEN_WIDTH * 0.7, 0) controlPoint2:CGPointMake(SCREEN_WIDTH * 0.85, 13)];
    [path addLineToPoint:CGPointMake(SCREEN_WIDTH, 40)];
    [path addLineToPoint:CGPointMake(0, 40)];
    [path addLineToPoint:CGPointMake(0, 37)];
    [path closePath];
    [path moveToPoint:CGPointMake(0, 37)];
    return path;
}


@end
